package designprinciples.singleresponsibility;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @program: MyTank
 * @description:
 * @author: 王术平
 * @create: 2021-01-23 17:12
 **/
public class PositiveExample {
    public static void main(String[] args) throws IOException {
        //加载文件
        StringBuilder sb = loadFile("D:\\架构师养成记\\Code\\MyTank\\src\\main\\java\\designprinciples\\singleresponsibility\\1.txt");
        //切割文件，统计单词数量
        int count = countNumberOfStringWords(sb.toString(), "[^a-zA-Z]+");
        System.out.println("该文件共有" + count + "个单词");
    }


    private static int countNumberOfStringWords(String str, String regexp) {
        String[] words = str.split(regexp);
        return words.length;
    }

    public static StringBuilder loadFile(String path) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(path));
        //读取文件，加入到StringBuilder中
        StringBuilder sb = new StringBuilder();
        for (String line = null; (line = reader.readLine()) != null; ) {
            sb.append(line + " \n");
        }
        if (reader != null) reader.close();
        return sb;
    }
}
